//
//  MultipleChooseControl.m
//  MultipleDemo
//
//  Created by Fortsven on 15/2/9.
//  Copyright (c) 2015年 Fortsven. All rights reserved.
//

#import "MultipleChooseControl.h"
@interface MultipleChooseControl()
{
    CGRect aFrame;
    BOOL isChoose;
    UIButton *button;
    UICollectionView *collection;
    NSMutableArray *itemArray;
}
@end

@implementation MultipleChooseControl

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        aFrame = frame;
        self.titleArray = [[NSArray alloc] init];
        self.chooseArray = [[NSMutableArray alloc] init];
        isChoose = NO;
        itemArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    [self createMultipleChoiseContent:CGRectMake(0, 0, aFrame.size.width, aFrame.size.height)];
}
- (void)createMultipleChoiseContent:(CGRect)rect
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    collection = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
    collection.delegate = self;
    collection.dataSource = self;
    collection.backgroundColor = [UIColor clearColor];
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"mcCell"];
    [self addSubview:collection];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mcCell" forIndexPath:indexPath];
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(5, 8, 14, 14);
    button.tag = indexPath.item + 8000;
    [itemArray addObject:button];

    [button setImage:[UIImage imageNamed:@"icon_没选中"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"icon_选中喜爱的"] forState:UIControlStateSelected];
    
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:button];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(25, 5, 60, 20)];
    title.text = self.titleArray[indexPath.item];
    title.font = [UIFont fontWithName:HEITILIGHT size:12];
    title.textColor = [UIColor grayColor];
    [cell addSubview:title];
    [cell sizeToFit];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return CGSizeMake(80, 30);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%zd", indexPath.row);
}

- (void)buttonPressed:(UIButton *)sender
{
    for (int i = 0; i < itemArray.count; i ++) {
        UIButton *btn = itemArray[i];
        if (btn.tag == sender.tag) {
            if (sender.selected == YES) {
                [self.chooseArray removeObject :@(sender.tag-8000)];
            } else {
                [self.chooseArray addObject:@(sender.tag-8000)];
            }
            sender.selected = !sender.selected;
            break;
        }
    }
    
    
    
}
@end
